﻿using System.Collections.Generic;
using System.Linq;
using Meraz.Core.Model;
using System.Xml.Linq;
using System.IO;
namespace Meraz.Core.Parsers
{
    public class TwitterXmlParser
    {
        DateTimeParser dtParser = new DateTimeParser();
        public TwitterXmlParser() { }

        public IEnumerable<XElement> GetElementList(string timelineUrl)
        {
            XElement doc = XElement.Load(timelineUrl);
            IEnumerable<XElement> childList =
                from el in doc.Elements()
                select el;
            return childList.ToList();
        }
        public IEnumerable<XElement> GetElementList(Stream stream)
        {
            XElement doc = XElement.Load(stream);
            IEnumerable<XElement> childList =
                from el in doc.Elements()
                select el;
            return childList.ToList();
        }
        public List<TweetValues> GetStatuses(IEnumerable<XElement> elements)
        {
            List<TweetValues> list = new List<TweetValues>();
            foreach (XElement e in elements)
            {
                TweetValues status = new TweetValues();
                status.Published = (string)e.Element("created_at");
                status.SetPubdate(status.Published);
                status.TweetID = (long)e.Element("id");
                status.Text = (string)e.Element("text");
                status.InReplyToScreenName = (string)e.Element("in_reply_to_screen_name");
                status.InReplyToStatusID = (string)e.Element("in_reply_to_status_id");
                status.InReplyToUserID = (string)e.Element("in_reply_to_user_id");
                status.Retweeted = (string)e.Element("retweeted");
                status.Source = (string)e.Element("source");
                status.UserID = (int)(e.Element("user").Element("id"));
                XElement entities = e.Element("entities");
                int sequence = 0;
                foreach (XElement mention in entities.Element("user_mentions").Elements("user_mention"))
                {
                    TwitterMention mensch = new TwitterMention();
                    mensch.Sequence = ++sequence;
                    mensch.TweetID = status.TweetID;
                    mensch.UserID = (int)mention.Element("id");
                    mensch.ScreenName = (string)mention.Element("screen_name");
                    mensch.Name = (string)mention.Element("name");
                    mensch.SetAttributes(status.Text);
                    status.Mentions.Add(mensch);
                }
                foreach (XElement urlElement in entities.Element("urls").Elements("url"))
                {
                    string url = (string)urlElement.Element("url");
                    status.Links.Add(url);
                }
                foreach (XElement hashElement in entities.Element("hashtags").Elements("hashtag"))
                {
                    string hashtag = (string)hashElement.Element("text");
                    status.Tags.Add(hashtag);
                }
                list.Add(status);
            }
            return list;
        }

    }
}
